package com.jsnkj.basic.activity

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.fragment.app.Fragment
import com.jsnkj.basic.R
import com.jsnkj.basic.databinding.ActivityMyEarningsBinding
import com.jsnkj.basic.fragment.earnings.DailyBillingFragment
import com.jsnkj.basic.fragment.earnings.InvitingNewFragment
import com.jsnkj.basic.mvvm.BaseKtActivity


//我的收益
class MyEarningsActivity : BaseKtActivity<ActivityMyEarningsBinding, MyEarningsViewModel>(),
    View.OnClickListener {
    override val layoutId: Int
        get() = R.layout.activity_my_earnings



    var tabTitle = arrayListOf("日账单", "邀新奖励")
    val listFragment: MutableList<Fragment> = ArrayList()


    private val bannerHandler = Handler(Looper.getMainLooper())
    private var bannerRunnable: Runnable? = null

    override fun initView(savedInstanceState: Bundle?) {
        mVDB.clickListener = this

        listFragment.add(DailyBillingFragment.newInstance())
        listFragment.add(InvitingNewFragment.newInstance())

        val viewPagerAdapter =
            com.jsnkj.basic.adapter.ReceiptsAdapter(
                supportFragmentManager,
                listFragment,
                tabTitle
            )
        mVDB.viewPager.setAdapter(viewPagerAdapter)
        mVDB.tabLayout.setupWithViewPager(mVDB.viewPager)

        // 初始化 Runnable 对象
        bannerRunnable = Runnable { // 这里写定时器需要执行的任务
            initBannerAd()
        }
        // 开始定时器
        bannerHandler.postDelayed(bannerRunnable!!, 3000)

        mVM.postApprentice()
    }

    override fun bindEvent() {
        super.bindEvent()
        mVM.postHttpResult.observe(this) { entity ->
            mVDB.txtTotalRevenue.text = "${entity.total_commissions}元"
            mVDB.txtPromotionIncentive.text = "${entity.popularize_awards}"
            mVDB.txtActivitySubsidy.text = "${entity.activity_awards}"
            mVDB.txtNewRewards.text = "${entity.invite_awards}"
        }
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.image_return -> {
                onBackPressed()
            }
        }
    }


    private fun initBannerAd() {
//        AdvertisementManager.loadBanner(
//            this@MyEarningsActivity,
//            SharePreferencesUtil.getInstance().getInt("app_screenWidth", 350) - 40,
//            mVDB.flContainer,
//            object : TbManager.BannerLoadListener {
//                override fun onFail(s: String) {
//                    BurialPointUtil.bannerAdEvent("fail")
//                    LogUtils.e("banner广告 加载失败：$s")
//                    mVDB.flContainer.visibility = View.INVISIBLE
//                }
//
//                override fun onDismiss() {
//                    mVDB.flContainer.visibility = View.INVISIBLE
//                }
//
//                override fun onClicked() = Unit
//                override fun onExposure(position: Position) {
//                    BurialPointUtil.bannerAdEvent("show")
//
//                    RewardsUtil.addGoldNumber(position.ecpm, RewardsConfig.adBanner)
//                }
//            })
    }


    override fun onPause() {
        super.onPause()
        // 取消定时器，避免内存泄漏
        bannerHandler.removeCallbacks(bannerRunnable!!)
    }

}